﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;
using Huazhu.PMS.Framework.Core;

namespace Huazhu.PMS.Framework.Organization
{
    public class OrgTreeProvider
    {
        private static OrgTree _tree;
        private static object syncRoot = new object();
        internal static OrgTree Tree
        {
            set { _tree = value; }
        }
        public static OrgTree GetOrganizationTree()
        {
            if (_tree == null)
            {
                lock (syncRoot)
                {
                    if (_tree == null)
                    {
                        OrgTree tree = BuildTree();
                        System.Threading.Thread.MemoryBarrier();
                        _tree = tree;
                    }
                }
            }
            return _tree;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static OrgTree BuildTree()
        {
            //OrgTree tree = new OrgTree();
            //SortedDictionary<string, OrgTreeNode> tnList = new SortedDictionary<string, OrgTreeNode>();
            ////-1代表了根节点
            //tnList.Add(OrgTree.ROOT_NODE_ID, tree.RootNode);
            //SortedDictionary<string, List<OrgTreeNode>> waitList = new SortedDictionary<string, List<OrgTreeNode>>();
            //List<OrgInfo> orgInfoList = new OrgInfoDataAccess().GetOrgInfo("OrgType");
            //foreach (OrgInfo orgInfo in orgInfoList)
            //{
            //    OrgTreeNode node = OrgTree.CreateNode(orgInfo.OrgID, orgInfo.OrgName);
            //    string parentOrgId = orgInfo.ParentOrgID;
            //    if (parentOrgId == null)
            //    {
            //        parentOrgId = OrgTree.ROOT_NODE_ID;
            //    }
            //    //父节点存在
            //    if (tnList.ContainsKey(parentOrgId))
            //    {
            //        tnList[parentOrgId].Append(node);
            //    }//父节点还未被加载，进入waitlist
            //    else
            //    {
            //        if (!waitList.ContainsKey(parentOrgId))
            //        {
            //            waitList.Add(parentOrgId, new List<OrgTreeNode>());
            //        }
            //        waitList[parentOrgId].Add(node);
            //    }
            //    tnList.Add(orgInfo.OrgID, node);
            //    tree.NodesOnTree.Add(orgInfo.OrgID, node);
            //    if (!string.IsNullOrEmpty(node.AreaId) && !tree.AreaIdIndex.ContainsKey(node.AreaId))
            //    {
            //        tree.AreaIdIndex.Add(node.AreaId, new List<OrgTreeNode>());
            //        tree.AreaIdIndex[node.AreaId].Add(node);
            //    }
            //    //检查是否有属于自己子节点在wait list中等候
            //    if (waitList.ContainsKey(orgInfo.OrgID))
            //    {
            //        foreach (OrgTreeNode subNode in waitList[orgInfo.OrgID])
            //        {
            //            node.Append(subNode);
            //        }
            //        waitList.Remove(orgInfo.OrgID);
            //    }
            //}
            return new OrgTree();
        }
    }
}
